package com.guinea.account.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.account.entity.Role;
import com.guinea.account.service.RoleManager;
import com.guinea.core.service.CRUDManager;
import com.guinea.core.web.StrutsEntityAction;

public class RoleAction extends StrutsEntityAction {
	
	private RoleManager roleManager;

	public CRUDManager getEntityManager() {
		return (CRUDManager) getRoleManager();
	}

	public RoleManager getRoleManager() {
		return roleManager;
	}

	public void setRoleManager(RoleManager roleManager) {
		this.roleManager = roleManager;
	}
	
	/**
	 * 删除单个对象的Action函数.
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Object object = initEntity(request);
		if(object == null){
			saveDirectlyError(request, "操作失败：记录不存在");
			return list(mapping, form, request, response);
		}
		try {
			doDeleteEntity(request,object);
			saveMessage(request, "entity.deleted");
		} catch (Exception e) {
			saveDirectlyError(request, e.getMessage());
		}
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 保存业务对象的函数.
	 */
	public void doSaveEntity(HttpServletRequest request,Object object) {
		getRoleManager().save(object);
		request.getSession().getServletContext().setAttribute("allRoles", getEntityManager().getValid());
	}

	/**
	 * 标记删除业务对象的函数.
	 */
	public void doEraseEntity(HttpServletRequest request,Object object) {
		getRoleManager().eraseRole((Role)object);
		request.getSession().getServletContext().setAttribute("allRoles", getEntityManager().getValid());
	}

	/**
	 * 删除业务对象的函数.
	 */
	public void doDeleteEntity(HttpServletRequest request,Object object) {
		getRoleManager().deleteById(id);
		request.getSession().getServletContext().setAttribute("allRoles", getEntityManager().getValid());
	}
}
